package com.nwork.cateye;

import android.content.ContentValues;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.nwork.cateye.model.CityModel;
import com.nwork.cateye.utils.CommonUtils;
import com.nwork.cateye.utils.DBHelper;
import com.nwork.cateye.utils.DatabaseUtils;
import com.nwork.cateye.utils.PinYin;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


public class CityListActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_list);
        String pinYin = PinYin.getPinYin("abcd");
        Log.d("**", "pinyin-->" + pinYin);
    }


    /**
     * 从Assets文件中得到Json字符串 并解析存入一个List集合中
     */
    private void getCityList() {
        AssetManager manager = getResources().getAssets();
        try {
            InputStream inputStream = manager.open("json/" + "cityJson.json");
            String jsonString = CommonUtils.inputToString(inputStream);
            JSONObject jsonObject = new JSONObject(jsonString);
            CityModel cityModel = new CityModel();
            cityModel.parseJson(jsonObject);
            List<CityModel> modelList = cityModel.getCityModelList();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将所有的城市信息插入到数据库中
     *
     * @param models
     */
    private void insertCityToDB(List<CityModel> models) {
        if (models != null) {
            DBHelper helper = new DBHelper(this);
            SQLiteDatabase database = helper.getWritableDatabase();
            database.beginTransaction();
            ContentValues values;
            for (int i = 0; i < models.size(); i++) {
                values = new ContentValues();
                CityModel cityModel = models.get(i);
                values.put("provence_name", cityModel.getProvenceName());
                values.put("city_name", cityModel.getCityName());
                values.put("city_code", cityModel.getCityCode());
                DatabaseUtils.insertData(database, values);

            }
            database.setTransactionSuccessful();
            database.endTransaction();
        }
    }


}
